package com.fr.cell;

import com.fr.base.FRContext;
import com.fr.cell.undo.UndoState;
import com.fr.report.ParameterReport;
import com.fr.report.TemplateWorkBook;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ParameterJWorkSheet extends ReportPane
{
  private ParameterReport paramReport;

  public ParameterJWorkSheet()
  {
    this(new ParameterReport());
  }

  public ParameterJWorkSheet(ParameterReport paramParameterReport)
  {
    setParameterReport(paramParameterReport);
    undoRecord();
  }

  public TemplateWorkBook getTemplate()
  {
    throw new UnsupportedOperationException();
  }

  public ParameterReport getParameterReport()
  {
    return this.paramReport;
  }

  public void setParameterReport(ParameterReport paramParameterReport)
  {
    this.paramReport = paramParameterReport;
    setEditingReport(this.paramReport);
  }

  protected UndoState createUndoState()
  {
    if (getEditingReport() == null)
      return null;
    GridSelection localGridSelection = null;
    try
    {
      localGridSelection = (GridSelection)getGridSelection().clone();
    }
    catch (CloneNotSupportedException localCloneNotSupportedException)
    {
      FRContext.getLogger().log(Level.WARNING, localCloneNotSupportedException.getMessage(), localCloneNotSupportedException);
    }
    int i = getGrid().getVerticalValue();
    int j = getGrid().getHorizontalValue();
    return new ReportState(this, this, localGridSelection, i, j, isUndoCacheable());
  }

  protected void changeBackUpTempalte(TemplateWorkBook paramTemplateWorkBook)
  {
    throw new UnsupportedOperationException();
  }

  public TemplateWorkBook getBackUpTempalte()
  {
    throw new UnsupportedOperationException();
  }

  public void setBackUpTemplate(TemplateWorkBook paramTemplateWorkBook)
  {
    throw new UnsupportedOperationException();
  }

  private class ReportState extends UndoState
    implements Serializable, Cloneable
  {
    private ParameterReport paramReport;
    private final ParameterJWorkSheet this$0;

    public ReportState(, ParameterJWorkSheet paramParameterJWorkSheet2, GridSelection paramGridSelection, int paramInt1, int paramInt2, boolean paramBoolean)
    {
      super(paramParameterJWorkSheet2, paramGridSelection, paramInt1, paramInt2, paramBoolean);
      this.this$0 = paramParameterJWorkSheet1;
    }

    protected void evaluateReportTemplate()
    {
      try
      {
        this.paramReport = ((ParameterReport)((ParameterJWorkSheet)this.reportPane).getParameterReport().clone());
      }
      catch (CloneNotSupportedException localCloneNotSupportedException)
      {
        FRContext.getLogger().log(Level.WARNING, localCloneNotSupportedException.getMessage(), localCloneNotSupportedException);
        return;
      }
      if (this.isCacheable)
        try
        {
          this.cacheFile = new File(getCacheDirectory(), "Undo" + System.currentTimeMillis() + "_" + (int)(Math.random() * 1000.0D));
          for (int i = 0; this.cacheFile.exists(); ++i)
            this.cacheFile = new File(getCacheDirectory(), "Undo" + System.currentTimeMillis() + "_" + (int)(Math.random() * 1000.0D) + i);
          FileOutputStream localFileOutputStream = new FileOutputStream(this.cacheFile);
          ObjectOutputStream localObjectOutputStream = new ObjectOutputStream(localFileOutputStream);
          localObjectOutputStream.writeObject(this.paramReport);
          localObjectOutputStream.flush();
          localObjectOutputStream.close();
          this.cacheFile.deleteOnExit();
          this.paramReport = null;
        }
        catch (Throwable localThrowable)
        {
        }
    }

    public void applyState()
    {
      ParameterReport localParameterReport = getParameterReport();
      if (localParameterReport == null)
        return;
      try
      {
        ((ParameterJWorkSheet)this.reportPane).setParameterReport((ParameterReport)localParameterReport.clone());
      }
      catch (CloneNotSupportedException localCloneNotSupportedException)
      {
        FRContext.getLogger().log(Level.WARNING, localCloneNotSupportedException.getMessage(), localCloneNotSupportedException);
      }
      super.applyState();
    }

    public ParameterReport getParameterReport()
    {
      if ((this.isCacheable) && (this.paramReport == null))
      {
        if ((this.cacheFile == null) || (!(this.cacheFile.exists())))
          return null;
        try
        {
          ObjectInputStream localObjectInputStream = new ObjectInputStream(new FileInputStream(this.cacheFile));
          ParameterReport localParameterReport = (ParameterReport)localObjectInputStream.readObject();
          localObjectInputStream.close();
          return localParameterReport;
        }
        catch (Throwable localThrowable)
        {
          FRContext.getLogger().log(Level.WARNING, localThrowable.getMessage(), localThrowable);
        }
      }
      return this.paramReport;
    }
  }
}